// 导入用户集合
const { Message } = require('../../../model/message');
const { decodeToken } = require('../../../middleGuard/jwt')
const { userProps } = require('../../../populate')

module.exports = async (req, res) => {
  const { toUser } = req.params
  const fromUser = decodeToken(req.headers.authorization).userId
  console.log('fromUser', fromUser)
  console.log('toUser', toUser)
  const list = await Message.find({ toUser, fromUser }).populate('fromUser', userProps).populate('toUser', userProps)
  const list2 = await Message.find({ toUser: fromUser, fromUser: toUser }).populate('fromUser', userProps).populate('toUser', userProps)
  
  let sortList = [...list, ...list2]
  sortList = sortList.sort((next, pre) => next.createTime - pre.createTime )
  res.json({
      data: sortList
  })
}